<?php


namespace App\Http\Controllers\Order;


use App\Http\Controllers\Controller;
use App\Http\Requests\Order\ShipmentMain\StoreRequest;
use App\Http\Requests\Order\ShipmentMain\UpdateRequest;
use App\Http\Searchs\Order\ShipmentMainSearch;
use App\Repositories\Shipment;
use Illuminate\Support\Facades\DB;

class ShipmentMainController extends Controller
{
    /**
     * @var Shipment
     */
    private $shipment;

    /**
     * ShipmentMainController constructor.
     * @param Shipment $shipment
     */
    public function __construct(Shipment $shipment)
    {
        $this->shipment = $shipment;
    }

    /**
     * @param ShipmentMainSearch $search
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(ShipmentMainSearch $search)
    {
        return $this->response(
            $this->shipment->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     * @author luffyzhao@vip.126.com
     */
    public function showCode(UpdateRequest $request, $id)
    {
        return $this->response(
            $this->shipment->showCode($id, $request->only(['express_code', 'weight']))
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(StoreRequest $request)
    {
        return $this->response(
            $this->shipment->create($request->only(['express_id']))
        );
    }

    /**
     * @param UpdateRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function update(UpdateRequest $request, $id)
    {
        return $this->response(
            DB::transaction(function () use ($request, $id) {
                $this->shipment->update($id, $request->only(['express_code', 'weight']));
            })
        );
    }
}
